<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>check demo</title>
  <link href="https://cdn.bootcdn.net/ajax/libs/normalize/8.0.1/normalize.min.css" rel="stylesheet">
  <link href="./css/view.css" rel="stylesheet">
  <link href="./css/editor.css" rel="stylesheet">

  <link href="../dist/css/style.css" rel="stylesheet">
</head>
<body>
  <p>视频、图片等资源校验，自定义 alert，XSS 预防</p>

  <!-- 编辑器 -->
  <div style="width: 950px; margin: 0 auto;">
    <div id="editor-toolbar" class="editor-toolbar"></div>
    <div id="editor-text-area" class="editor-text-area"></div>
  </div>

  <script src="js/init-content.js"></script>
  <script src="../dist/index.js"></script>
  <script>
    const E = window.wangEditor

    const editorConfig = { MENU_CONF: {} }
    editorConfig.placeholder = '请输入内容'
    
    editorConfig.customAlert = (info, type) => {
      alert(`customAlert: \n${type}:\n${info}`)
    }

    editorConfig.MENU_CONF['uploadImage'] = {
      server: 'http://106.12.198.214:3000/api/upload-img',
    }

    editorConfig.MENU_CONF['insertImage'] = {
      onInsertedImage(imageNode) {
        console.log('inserted image', imageNode)
      },
      checkImage(src, alt, url) {
        if (src.indexOf('http') !== 0) {
          return '图片网址必须以 http/https 开头'
        }
        return true
      },

      // // 异步检查
      // async checkImage(src, alt, url) {
      //   return new Promise((resolve, reject) => {
      //     setTimeout(() => {
      //       if (src.indexOf('http') !== 0) {
      //         resolve('图片网址必须以 http/https 开头')
      //         return
      //       }
      //       resolve(true)
      //     }, 1000)
      //   })
      // },

      parseImageSrc(src) {
        return src + '#123'
      },

      // // 异步转换
      // async parseImageSrc(src) {
      //   return new Promise((resolve, reject) => {
      //     setTimeout(() => {
      //       resolve(src + '#abc')
      //     }, 1000)
      //   })
      // },
    }
    editorConfig.MENU_CONF['editImage'] = {
      onUpdatedImage(imageNode) {
        console.log('updated image', imageNode)
      },
      checkImage(src, alt, url) {
        if (src.indexOf('http') !== 0) {
          return '图片网址必须以 http/https 开头'
        }
        return true
      },

      // // 异步检查
      // async checkImage(src, alt, url) {
      //   return new Promise((resolve, reject) => {
      //     setTimeout(() => {
      //       if (src.indexOf('http') !== 0) {
      //         resolve('图片网址必须以 http/https 开头')
      //         return
      //       }
      //       resolve(true)
      //     }, 1000)
      //   })
      // },

      parseImageSrc(src) {
        return src + '#123'
      },
    }
    editorConfig.MENU_CONF['insertLink'] = {
      checkLink(text, url) {
        console.log('check insert link - ', text, url)

        if (url.indexOf('http') !== 0) {
          return '链接必须以 http/https 开头'
        }
        return true
      },

      // // 异步检查
      // async checkLink(text, url) {
      //   console.log('check insert link - ', text, url)

      //   return new Promise((resolve, reject) => {
      //     setTimeout(() => {
      //       if (url.indexOf('http') !== 0) {
      //         resolve('链接必须以 http/https 开头')
      //         return
      //       }
      //       resolve(true)
      //     }, 1000)
      //   })
      // },

      parseLinkUrl(url) {
        return url + '#123'
      },

      // // 异步转换
      // async parseLinkUrl(url) {
      //   console.log('parse insert link - ', url)

      //   return new Promise((resolve, reject) => {
      //     setTimeout(() => {
      //       resolve(url + '#123')
      //     }, 1000)
      //   })
      // },
    }
    editorConfig.MENU_CONF['editLink'] = {
      checkLink(text, url) {
        if (url.indexOf('http') !== 0) {
          return '链接必须以 http/https 开头'
        }
        return true
      },

      // // 异步检查
      // async checkLink(text, url) {
      //   console.log('check insert link - ', text, url)

      //   return new Promise((resolve, reject) => {
      //     setTimeout(() => {
      //       if (url.indexOf('http') !== 0) {
      //         resolve('链接必须以 http/https 开头')
      //         return
      //       }
      //       resolve(true)
      //     }, 1000)
      //   })
      // },

      parseLinkUrl(url) {
        return url + '#123'
      },
    }
    editorConfig.MENU_CONF['insertVideo'] = {
      onInsertedVideo(videoNode) {
        console.log('inserted video', videoNode)
      },

      checkVideo(src) {
        if (src.indexOf('http') !== 0) {
          return '视频地址必须以 http/https 开头'
        }
        return true
      },

      // // 异步检查
      // async checkVideo(src) {
      //   return new Promise((resolve, reject) => {
      //     setTimeout(() => {
      //       if (src.indexOf('http') !== 0) {
      //         resolve('视频地址必须以 http/https 开头')
      //         return
      //       }
      //       resolve(true)
      //     }, 1000)
      //   })
      // },

      // 也支持 promise
      parseVideoSrc(src) {
        if (src.includes('.bilibili.com')) {
          // 转换 bilibili url 为 iframe
          const arr = location.pathname.split('/')
          const vid = arr[arr.length - 1]
          return `<iframe src="//player.bilibili.com/player.html?aid=421814407&bvid=${vid}" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"> </iframe>`
        }
        return src
      }
    }

    const editor = E.createEditor({
      selector: '#editor-text-area',
      content: window.content1,
      config: editorConfig
    })
    const toolbar = E.createToolbar({
      editor,
      selector: '#editor-toolbar',
    })
  </script>
</body>
</html>